(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.custAcct.createRejectController', createRejectController);
	createRejectController.$inject = [ '$scope', '$q', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'custAcctService', 'storageService'];
	function createRejectController($scope, $q, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, custAcctService, storageService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {}, datas = [], datasTwo = [];
		ctrl.layoutConfig = layoutConfig;
		var layout1=ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions;
		var layout2=ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions;
		ctrl.validatorConfig = validatorConfig;
		ctrl.custAcct = {};
		custAcctService.initFunction(ctrl);

		ctrl.init = function() {
			custAcctService.initOptions().then(function(response){
				ctrl.custAcctTypeOptions = response.custAcctTypeOptions;
				ctrl.ccyOptions = response.ccyOptions;
				ctrl.paycodeCatOptions = response.paycodeCatOptions;
				ctrl.allPartyTypeOptions = response.allPartyTypeOptions;
				ctrl.yesNoOptions = response.yesNoOptions;
				ctrl.yesNoOptionsb = response.yesNoOptionsb;
				ctrl.swiftCaseList = response.swiftCaseList;
				ctrl.accountPatterns = response.accountPatterns;
				ctrl.accountPatternsTwo = response.accountPatternsTwo;
				
				ctrl.partyCountrys = response.countryOptionAll;
				ctrl.partyStates = response.stateOptionAll;
				ctrl.partyCitys = response.cityOptionAll;
				ctrl.stateList = response.stateList;
				ctrl.cityList = response.cityList;
				ctrl.partyStatesCatch = response.stateOptionAll;
				ctrl.partyCitysCatch = response.cityOptionAll;
				
				datas = angular.copy(response.accountPatterns);
				datasTwo = angular.copy(response.accountPatterns);
				
				authService.createReject(params).then(function(response) {
					if (response.data && response.data.model && response.data.task) {
						ctrl.custAcct = response.data.model;
						ctrl.custAcct.taskId = response.data.task.taskId;
						ctrl.custAcct.siteCode = storageService.getUserInfo().siteCode;
						ctrl.custAcct.custAcctSwift = ctrl.custAcct.custAcctSwift || {};
						ctrl.custAcct.custAcctSwift.payPartyList = ctrl.custAcct.custAcctSwift.payPartyList || [];
						ctrl.initPyPartyTabs();
						ctrl.judge="Y";
					}
				});
			});
		};
		
		
		ctrl.setPatternNames = function() {
			if(ctrl.judge =="Y"){
				if (ctrl.custAcct.acctPatternFlag == "N") {
					delete ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions;
					delete ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions;
				} else {
					ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions = layout1;
					ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions = layout2;
				}
				ctrl.judge="N";
			}else if(ctrl.judge =="N"){
				if (ctrl.custAcct.acctPatternFlag == "N") {
					delete ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions;
					delete ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions;
					ctrl.custAcct.acctNoPrefix = null;
					ctrl.custAcct.acctNoSuffix = null;
					ctrl.acctChanged();
					ctrl.acctChangedTwo();
					
				} else {
					ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions = layout1;
					ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions = layout2;
					ctrl.custAcct.acctNoPrefix = null;
					ctrl.custAcct.acctNoSuffix = null;
					ctrl.acctChanged();
					ctrl.acctChangedTwo();
					
				}
			}
		};
		
		ctrl.acctChanged = function(){
			ctrl.accountPatterns = [];
			for(var j=0; j < datas.length; j++){
				ctrl.accountPatterns.push(datas[j]);
			}
			if(ctrl.accountPatterns.length > 0 && ctrl.custAcct.acctNoPrefix != null){
				var m=[], count=[];
				 for(var i=0; i < ctrl.accountPatterns.length; i++){
					 count.push(i);
					 if(ctrl.custAcct.acctNoPrefix.indexOf(ctrl.accountPatterns[i].value) != -1){
						m.push(i);
					 }
				 }
				if(m.length > 0){
					var purify = function(arr1,arr2){
					  var as = [];
					  var arr ="."+arr1.join(".")+".";
					    for(var k=0; k<arr2.length; k++){
					        arr.indexOf("."+arr2[k]+".") === -1 ? as.push(datas[k]) : 0; 
					    }  
					    return as;  
					};
					ctrl.accountPatterns = purify(m, count);
				} 
			}
		};

		ctrl.acctChangedTwo = function(){
			ctrl.accountPatternsTwo = [];
			for(var j=0; j < datasTwo.length; j++){
				ctrl.accountPatternsTwo.push(datasTwo[j]);
			}
			if(ctrl.accountPatternsTwo.length > 0 && ctrl.custAcct.acctNoSuffix != null){
				var m=[], count=[];
				 for(var i=0; i < ctrl.accountPatternsTwo.length; i++){
					 count.push(i);
					 if(ctrl.custAcct.acctNoSuffix.indexOf(ctrl.accountPatternsTwo[i].value) != -1){
						m.push(i);
					 }
				 }
				if(m.length > 0){
					var purify = function(arr1,arr2){
					  var as = [];
					  var arr ="."+ arr1.join(".")+".";
					    for(var k=0;k<arr2.length;k++){  
					        arr.indexOf("."+arr2[k]+".") === -1 ? as.push(datasTwo[k]) : 0;
					    }  
					    return as;  
					};
					ctrl.accountPatternsTwo = purify(m, count);
				} 
			}
		};
		
		ctrl.createRejectSave = function() {
			ctrl.checkPayPartyList();
			return authService.createRejectSave(ctrl.custAcct).then(saveSuccessful, saveFailed);
		};

		ctrl.createRejectSubmit = function() {
			ctrl.checkPayPartyList();
			return authService.createRejectSubmit(ctrl.custAcct).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.custAcct.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.custAcct).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.custAcct.list',
					'state' : 'cda.custAcct.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.custAcct, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.custAcct.list',
					'state' : 'cda.custAcct.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);
